package com.ebupt.migu.music.label.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.restapi.UploadBySFTP;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liuyangyang@ebupt.com
 * @description: TODO
 * @date 2021/9/2 15:52
 */
@Slf4j
@Component
public class SftpUtils {
    @Value("${sftp.music_ip}")
    private String MUSIC_IP;
    @Value("${sftp.music_port}")
    private int MUSIC_PORT;
    @Value("${sftp.music_user_name}")
    private String MUSIC_USER_NAME;
    @Value("${sftp.music_password}")
    private String MUSIC_PASSWORD;
    @Value("${sftp.music_path}")
    private String MUSIC_PATH;

    @Value("${sftp.label_ip}")
    private String LABEL_IP;
    @Value("${sftp.label_port}")
    private int LABEL_PORT;
    @Value("${sftp.label_user_name}")
    private String LABEL_USER_NAME;
    @Value("${sftp.label_password}")
    private String LABEL_PASSWORD;
    @Value("${sftp.label_path}")
    private String LABEL_PATH;

    @Value("${sftp.local_path}")
    private String LOCAL_PATH;

    /**
     * 临时客群上传
     *
     * @param fileName
     * @return
     */
    public boolean fileSyncToLabel(String fileName) {
        try {
            log.info("开始下载文件至本地：{}", fileName);
            boolean music = UploadBySFTP.getSftpFile(MUSIC_IP, MUSIC_PORT, MUSIC_USER_NAME, MUSIC_PASSWORD, MUSIC_PATH,
                LOCAL_PATH, fileName);
            if (!music) {
                log.error("SFTP下载文件至本地失败：{}", fileName);
                return false;
            }
            log.info("开始上传文件至统一标签服务平台：{}", fileName);
            boolean label = UploadBySFTP.putSftpFile(LABEL_IP, LABEL_PORT, LABEL_USER_NAME, LABEL_PASSWORD, LABEL_PATH,
                LOCAL_PATH, fileName);
            if (!label) {
                log.error("SFTP上传文件至统一标签服务平台失败：{}", fileName);
                return false;
            }
            return true;
        } catch (Exception e) {
            log.error("文件上传至统一标签服务平台失败：{}", e.getMessage());
            return false;
        }
    }

    /**
     * 将文件同步到TSG
     *
     * @param fileName 文件名
     * @return 是否同步成功
     */
    public boolean syncToLabel(String fileName) {
        double filesSize = FileSizeUtil.getFileOrFilesSize(LOCAL_PATH + fileName, 3);
        //
        double fileMaxSize = 100;
        if (filesSize > fileMaxSize) {
            throw new MyException(StatusEnum.BUSINID, "所选客群手机号已超过100M，无法分箱，请选择其他客群");
        }
        try {
            // 将文件同步至统一标签平台
            log.info("开始上传文件至TSG：{}", fileName);
            boolean b = UploadBySFTP.putSftpFile(LABEL_IP, LABEL_PORT, LABEL_USER_NAME, LABEL_PASSWORD, LABEL_PATH,
                LOCAL_PATH, fileName);
            if (!b) {
                log.info("SFTP上传文件至TSG失败：{}", fileName);
                return false;
            }
        } catch (Exception e) {
            log.info("文件上传至统一标签服务平台失败：{}", e.getMessage());
            return false;
        }
        return true;
    }
}